package com.example.home.fragment;

import static android.content.Intent.getIntent;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.home.activity.R;
import com.example.home.adapter.GoodsListAdapter;
import com.example.home.api.ApiService;
import com.example.home.interfaces.ResponseCallback;
import com.example.home.viewmodel.GoodsList.GoodsListData;
import com.example.home.viewmodel.ResponseData;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GoodsListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GoodsListFragment extends Fragment {
    private List<com.example.home.viewmodel.GoodsList.List> lists;

    private ListView viewGoodsList;
    private TextView goodslist_list_name;
    private TextView goodslist_list_desc;

    private long id;
    private String name;
    private String desc;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    public GoodsListFragment() {
        // Required empty public constructor
    }

    public static GoodsListFragment newInstance(String param1, String param2) {
        GoodsListFragment fragment = new GoodsListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_goods_list, container, false);
        viewGoodsList = view.findViewById(R.id.goodslist_list_view);
        goodslist_list_name = view.findViewById(R.id.goodslist_list_name);
        goodslist_list_desc = view.findViewById(R.id.goodslist_list_desc);
        if(getArguments()!=null){
            id = getArguments().getLong("id");
            name = getArguments().getString("name");
            desc = getArguments().getString("desc");
            Log.e("TA", "on"+mParam1 );
            Log.e("TA", "on"+id );
            goodslist_list_name.setText(name);
            goodslist_list_desc.setText(desc);
            ApiService.GetGoodsList(""+id, new ResponseCallback<ResponseData<GoodsListData>>() {
                @Override
                public void onSuccess(ResponseData<GoodsListData> result) {
                    if(result == null){
                        return;
                    }else {
                        lists = result.getData().getList();
                        if (lists != null && !lists.isEmpty()) {
                            viewGoodsList.setAdapter(new GoodsListAdapter(view.getContext(), lists));
                        }
                    }
                }
            });
        }

        return view;
    }
}